package com.bwt.usermanage.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwt.usermanage.Entity.User;
import com.bwt.usermanage.R;
import com.bwt.usermanage.utils.DateUtil;
import com.bwt.usermanage.utils.ViewUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.List;


public class UserAdapter extends BaseAdapter {
    Context context;
    List<User>mDatas;
    LayoutInflater inflater;
    int year,month,day;
    public UserAdapter(Context context, List<User> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        inflater = LayoutInflater.from(context);
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_mainlv,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        User bean = mDatas.get(position);
        if (StringUtils.isNotBlank(bean.borrowerProfilePic)) {

            Bitmap bitmap = BitmapFactory.decodeFile(bean.borrowerProfilePic);
            holder.iv_borrowerProfilePic.setImageBitmap(bitmap);
        }
        boolean f = DateUtil.todayCompare(bean.expireTime);
        holder.tv_borrowerName.setText(bean.borrowerName);
        if (f) {
            holder.tv_borrowerName.setTextColor(context.getResources().getColor(R.color.colorAccent));
            holder.tv_borrowerName.setText(bean.borrowerName + " (已过期)");
        }

        holder.tv_borrowerSFZ.setText(bean.borrowerSFZ);
        holder.tv_loansPrice.setText("￥ "+bean.loansPrice+" 万元");
        holder.tv_expireTime.setText(bean.expireTime);
        return convertView;
    }

    class ViewHolder{
        ImageView iv_borrowerProfilePic;
        TextView tv_borrowerName,tv_borrowerSFZ,tv_loansPrice,tv_expireTime;
        public ViewHolder(View view){
            iv_borrowerProfilePic = view.findViewById(R.id.item_mainlv_iv_borrowerProfilePic);
            tv_borrowerName = view.findViewById(R.id.item_mainlv_tv_borrowerName);
            tv_borrowerSFZ = view.findViewById(R.id.item_mainlv_tv_borrowerSFZ);
            tv_loansPrice = view.findViewById(R.id.item_mainlv_tv_loansPrice);
            tv_expireTime = view.findViewById(R.id.item_mainlv_tv_expireTime);

        }
    }
}
